/*****************************************************************************\

File:   otkCreateSection2D.java

Date          Build     Author        Version   Comments
28-Aug-12    P-20-13    asonar         $$1       Created
12-Oct-12    P-20-14    asonar         $$2       Updated 
20-mar-15 P-30-05 gshmelev $$3  called GetCurrentSession

\****************************************************************************/

package com.ptc.otkjavaexamples;

import com.ptc.cipjava.*;
import com.ptc.pfc.pfcSession.*;
import com.ptc.pfc.pfcCommand.*;
import com.ptc.pfc.pfcGlobal.*;
import com.ptc.pfc.pfcModel.*;
import com.ptc.pfc.pfcModelItem.*;
import com.ptc.pfc.pfcFeature.*;
import com.ptc.pfc.pfcSolid.*;
import java.io.*;

import com.ptc.pfc.pfcFamily.*;
import com.ptc.pfc.pfcWindow.*;
import com.ptc.pfc.pfcSection.*;
import com.ptc.wfc.wfcSection.*; 
import com.ptc.wfc.wfcSession.*;
import com.ptc.pfc.pfcBase.*;

/*====================================================================*\
FUNCTION : otkCreateSection2D
PURPOSE  : This example will demonstrate creating & manipulating 
	       Section2D as model.
		   It will first allocate a 2D section & set its name.
		   It will then call Save() on it to save it as .sec file.
		   It will then retrieve the .sec file & add/delete entities 
		   to/from it.
\*====================================================================*/

class otkCreateSection2D extends DefaultUICommandActionListener
{

    public otkCreateSection2D()
	{
	}
	
	public void OnCommand()
	{
	addCreateSection2D();
	}
	
	public void addCreateSection2D()
	{
      	try
		{
		
		
		FileWriter file = new FileWriter ("2D_Section.txt");
     	BufferedWriter fp_out_2D_Section = new BufferedWriter(file);
		
		Session session = pfcSession.GetCurrentSession ();      
        WSession wSession = (WSession)session;
      
        /*
	    Create a new Section2D file with extension .sec
	
	    NOTE 1: New file will be saved as otk_section_2d.sec
	
		NOTE 2: 2D Sections are always represented by .sec file
        */
        Section section2D = wSession.CreateSection2D();
      
        section2D.SetName("otk_section_2d");
      
        /*
		Add 2D Entities to the section
	
		NOTE : Before manipulating any section we must turn ON intent manager
		to ensure section resolution done properly
	
        */
      
		section2D.SetIntentManagerMode(true);
      
		AddEntitiestoSection(section2D);
		
		section2D.SetIntentManagerMode(false);
      
		/*
		Interface wfcSection inherits from wfcWModel hence method pfcModel::Save()
		will save otk_section_2d.sec to current directory
	
		NOTE : only 2D Sections are allowed to use wfcWModel methods
		They should not be used for 3D Sections
	
       */
		section2D.Save();
      
		session.EraseUndisplayedModels();
      
		/*
		Retrieve a 2D Section as pfcModel object
		Display it using pfcModel::Display()
		*/
      
		ModelDescriptor desc = pfcModel.ModelDescriptor_CreateFromFileName
		("otk_section_2d.sec");	  
      
		Model sectionModel = session.RetrieveModel(desc);
      
		sectionModel.Display();
      
		/*
		Convert pfcModel object to wfcSection
	
		NOTE : We can cast pfcModel or wfcWModel object whose type is 
		pfcMDL_2D_SECTION to wfcSection object       
		   
		*/
      
		Section section2d = (Section)sectionModel;  
		fp_out_2D_Section.write ("\nName of the Section " + section2d.GetName() );
		fp_out_2D_Section.newLine();
	  
		/*
		List Entities in the Section
		Delete an entity
		*/
      
		SectionEntities entities = section2d.ListSectionEntities();
		fp_out_2D_Section.write ("\nNumber of entities in the section " + 
		entities.getarraysize() );
		fp_out_2D_Section.newLine();
      
		for(int ii =0;ii<entities.getarraysize();ii++)
		{
		fp_out_2D_Section.write ("\n\t-Id of entity" + (ii+1) + " is " + 
		section2d.GetEntityIds().get(ii) );
		
		fp_out_2D_Section.newLine();
		}
      
		section2d.DeleteEntity(section2d.GetEntityIds().get(1));
		
		fp_out_2D_Section.close();
      
         }
		
		catch (Exception e)
		{
				System.out.println("1: " + e);
		}
		
	}
	
	public static void AddEntitiestoSection(Section section)
   {
  
      try
      {
      /*
	  wfcSectionEntity is parent class for all kind of entities
	
	  NOTE : We should always cast child entitiy object e.g.wfcSectionEntityPoint
	  to wfcSectionEntity before passing it to AddEntity()
	
      */
      
      Point2D point = wfcSession.CreatePoint2D(100.00,100.00);
	  
      SectionEntityPoint  pointEnt = wfcSection.SectionEntityPoint_Create
	  (point);
      SectionEntity entity = (SectionEntity)pointEnt;
      section.AddEntity(entity);
      
      Outline2D line = wfcSession.CreateOutline2D(10.00,10.00,150.00,150.00);
      
      SectionEntityLine lineEnt = wfcSection.SectionEntityLine_Create(line);
      entity = (SectionEntity)lineEnt;
      section.AddEntity(entity);
      
      SectionEntityCircle circleEnt = wfcSection.SectionEntityCircle_Create
	  (point,100.00);
      entity = (SectionEntity)circleEnt;
      section.AddEntity(entity);
      
      SectionEntityEllipse ellipseEnt = wfcSection.SectionEntityEllipse_Create
	  (point,50.00,20.00);
      entity = (SectionEntity)ellipseEnt;
      section.AddEntity(entity);
       }
  
		catch (Exception e)
		{
				System.out.println("1: " + e);
		}  
   
   }

}
